//
//  YZYStatusRetweetView.swift
//  weibo
//
//  Created by mac on 16/10/29.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYStatusRetweetView: UIView {

    // 记录约束
    var retweetViewCons: Constraint?
    
    // 定义属性 供外界赋值
    var statusViewModel: YZYStatusViewModel?{
        didSet{
            // 转发正文
            contentLabel.text = statusViewModel?.retweetContentText
            
            // 卸载约束
            /*
             - 如果配图数组. count > 0 就代表有配图
             - 赋值
             - 显示配图
             - 转发微博的 bottom == 配图的 bottom
             - 如果没有配图
             - 隐藏配图
             - 转发微博的 bottom == 文字的bottom
             */
            // 卸载约束
            retweetViewCons?.uninstall()
            // 有配图
            if (statusViewModel?.statusModel?.retweeted_status?.pic_urls?.count)! > 0 {
                pictureView.picUrls = statusViewModel?.statusModel?.retweeted_status?.pic_urls
                pictureView.isHidden = false
                
                self.snp_makeConstraints { (make) in
                    self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
            }else {
                // 没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints { (make) in
                    self.retweetViewCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 01 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 02 添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            //            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        
        self.snp_makeConstraints { (make) in
            self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    // MARK: - 懒加载控件
    // 转发正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: YZYNormalFontSize, text: "设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图设置视图", maxWidth: YZY_W - 20)
    // 配图
    private lazy var pictureView: YZYStatusPictrueView = {
        let view = YZYStatusPictrueView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}
